<template>
   <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
         <el-form-item label="数据集" prop="ds_id">
               <el-select v-model="queryParams.ds_id" placeholder="数据集" clearable style="width: 200px">
                  <el-option
                     v-for="dict in dsList"
                     :key="dict.id"
                     :label="dict.name"
                     :value="dict.id"
                  />
               </el-select>
         </el-form-item>
         <el-form-item label="摄像机" prop="camera_id">
               <el-select v-model="queryParams.camera_id" placeholder="摄像机" clearable style="width: 200px">
                  <el-option
                     v-for="dict in cameraList"
                     :key="dict.id"
                     :label="dict.name"
                     :value="dict.id"
                  />
               </el-select>
         </el-form-item>
         <el-form-item label="图片名称" prop="name">
            <el-input
               v-model="queryParams.name"
               placeholder="请输入采集图片名称"
               clearable
               style="width: 200px"
               @keyup.enter="handleQuery"
            />
         </el-form-item>
         <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="状态" clearable style="width: 200px">
               <el-option
                  v-for="dict in sys_status"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
               />
            </el-select>
         </el-form-item>
         <el-form-item label="分组" prop="category">
            <el-select v-model="queryParams.category" placeholder="分组" clearable style="width: 200px">
               <el-option
                  v-for="dict in sys_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
               />
            </el-select>
         </el-form-item>
         <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
            <el-button icon="Refresh" @click="resetQuery">重置</el-button>
         </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
         <el-col :span="1.5">
            <el-button
               type="primary"
               plain
               icon="Plus"
               @click="handleAdd('train')"
            >添加到训练集</el-button>
         </el-col>
         <el-col :span="1.5">
            <el-button
               type="primary"
               plain
               icon="Plus"
               @click="handleAdd('val')"
            >添加到验证集</el-button>
         </el-col>
         <el-col :span="1.5">
            <el-button
               type="primary"
               plain
               icon="Plus"
               @click="handleAdd('test')"
            >添加到测试集</el-button>
         </el-col>
         <el-col :span="1.5">
            <el-button
               type="danger"
               plain
               icon="Delete"
               :disabled="multiple"
               @click="handleDelete"
            >删除</el-button>
         </el-col>
         <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="collectionList" @selection-change="handleSelectionChange">
         <el-table-column type="selection" width="55" align="center" />
         <el-table-column label="图片" align="center" prop="name">
            <template #default="scope">
               <image-preview :src="'/images/view/'+scope.row.id" :width="80" :height="80"/>
            </template>
         </el-table-column>
         <el-table-column label="分组" align="center" prop="category" width="100">
            <template #default="scope">
               <dict-tag :options="sys_type" :value="scope.row.category" />
            </template>
         </el-table-column>
         <!--el-table-column label="标注人" align="center" prop="user_name" width="100" /-->
         <el-table-column label="状态" align="center" prop="status" width="100">
            <template #default="scope">
               <dict-tag :options="sys_status" :value="scope.row.status" />
            </template>
         </el-table-column>
         <el-table-column label="采集时间" align="center" prop="create_time" />
         <el-table-column label="标记时间" align="center" prop="update_time" />
         <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
            <template #default="scope">
               <el-button link type="primary" icon="View" @click="handleView(scope.row)">查看</el-button>
            </template>
         </el-table-column>
      </el-table>

      <pagination
         v-show="total > 0"
         :total="total"
         v-model:page="queryParams.page"
         v-model:limit="queryParams.limit"
         @pagination="getList"
      />

      <!-- 添加或修改公告对话框 -->
      <el-dialog :title="title" v-model="open" width="800px" append-to-body>           
         <div id="mark-container"></div>
         <template #footer>
            <div class="dialog-footer">
               <el-button @click="cancel">取 消</el-button>
            </div>
         </template>
      </el-dialog>
   </div>
</template>

<script setup name="Image">
import SimpleImageLabel from 'simple-image-label'
import { listImage, getImage, delImage, addImage, updateImage, moveImage } from "@/api/image";
import { listDataset } from "@/api/dataset";
import { listCamera } from "@/api/camera";



const { proxy } = getCurrentInstance();
const sys_status = [{value:'0', label:'未标记'}, {value:'1', label:'已标记'}, {value:'2', label:'已审核'}];
const sys_type = [{value:'train', label:'训练集'}, {value:'test', label:'测试集'}, {value:'val', label:'验证集'}];
const collectionList = ref([]);
const dsList = ref([]);
const cameraList = ref([]);

const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const baseURL = import.meta.env.VITE_APP_BASE_API
const simpleImageLabel = ref(null);

const data = reactive({
  form: {},
  queryParams: {
    page: 1,
    limit: 10,
    name: undefined,
    ds_id: undefined,
    camera_id: undefined,
    category: undefined,
    status: undefined
  },
  rules: {
    name: [{ required: true, message: "采集任务名称不能为空", trigger: "blur" }],
    status: [{ required: true, message: "状态不能为空", trigger: "change" }],
    camera_id: [{ required: true, message: "摄像机不能为空", trigger: "change" }],
    ds_id: [{ required: true, message: "数据集不能为空", trigger: "change" }],
  },
});

const { queryParams, form, rules } = toRefs(data);

/** 查询采集任务列表 */
function getList() {
  loading.value = true;
  listImage(queryParams.value).then(response => {
    collectionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}
/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}
/** 表单重置 */
function reset() {
  form.value = {
    name: undefined,
    camera_id: undefined,
    ds_id: undefined,
    status: "0"
  };
  proxy.resetForm("formRef");
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}
/** 多选框选中数据 */
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/**修改按钮操作 */
function handleView(row) {
   open.value = true;
   nextTick(() => {
      simpleImageLabel.value = new SimpleImageLabel({
         el: 'mark-container',
         imageUrl: baseURL + '/images/view/' + row.id,
         labels: row.annotations ? row.annotations : [],
         contextmenu: (e) => {
            console.log(e);
         },
         error: (e) => {
            console.log(e);
         }
      });
   });
}
/** 提交按钮 */
function handleAdd(category) {
  proxy.$modal.confirm('是否确认转移选中的图片到"' + sys_type.find(item => item.value == category).label + '"？').then(function() {
    return moveImage(category, ids.value);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 删除按钮操作 */
function handleDelete(row) {
  const imageId = row.id || ids.value
  proxy.$modal.confirm('是否确认删除图片编号为"' + imageId + '"的图片？').then(function() {
    return delImage(imageId);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();

listDataset().then(response => {
   dsList.value = response.rows;
});
listCamera().then(response => {
   cameraList.value = response.rows;
});


</script>
